/*
 * @Date         : 2024-05-12 17:40:38 星期0
 * @Author       : xut
 * @Description  :
 */
import { resolve } from "node:path"
import grpc from "@grpc/grpc-js"
import { loadProto } from "../utils/loadProto.js"
import { echoServiceImpl } from "./echoServiceImpl.js"

const PROTO_PATH = resolve(process.cwd(), "./echo.proto")
const echoProtoDescriptor = loadProto(PROTO_PATH)
const echoProto = echoProtoDescriptor.echo
const server = new grpc.Server()

server.addService(echoProto.Echo.service, echoServiceImpl)
server.bindAsync(
  "localhost:50051",
  grpc.ServerCredentials.createInsecure(),
  (err, port) => {
    if (err !== null) {
      return console.error(err)
    }

    console.log("gRPC listening on localhost:" + port)
  }
)
